/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package base;

import java.util.Timer;
import java.util.TimerTask;
import tal.drivechain.matchers.VarMatcher;
import tal.drivechain.plugin.Channel;
import tal.drivechain.plugin.Module;
import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.SetProperty.Flag;
import tal.drivechain.variable.Variable;

/**
 *
 * @author Main
 */
public class Stream extends Module {
    
    private VarMatcher trigger, stopTrigger, startTrigger;
    private long sampleRate; // in miliseconds
    private double curValue;
    private Timer frameTimer;
    private Variable streamVar;
    
    @Override
    public void setupProperties() throws IllegalArgumentException {
        setTrigger(props.getProperty("trigger"), null);
        setSampleRate(props.getProperty("sampleRate"), null);
        setStopTrigger(props.getProperty("stopTrigger"), null);
        setStartTrigger(props.getProperty("startTrigger"), null);
        curValue = 0;
        streamVar = new Variable("stream", DataType.FLOAT, null, true);
        streamVar.setValue(curValue, true);
    }

    public void setTrigger(Object value, Flag flag) {
        trigger = (VarMatcher)value;
    } public Object getTrigger() { return trigger; }

    public void setStartTrigger(Object value, Flag flag) {
        startTrigger = (VarMatcher)value;
    } public Object getStartTrigger() { return startTrigger; }

    public void setStopTrigger(Object value, Flag flag) {
        stopTrigger = (VarMatcher)value;
    } public Object getStopTrigger() { return stopTrigger; }

    public void setSampleRate(Object value, Flag flag) {
        sampleRate = (Long)value;
        if (frameTimer!=null) {
            stopStream();
            startStream();
        }
    } public Object getSampleRate() { return sampleRate; }

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        if (trigger.matches(in, channel, this)) {
            try {
                if (in.getDataType().matches(DataType.FLOAT))
                    streamVar.setValue(in.getValue(this), true);
                else 
                    streamVar.setValue(DataType.FLOAT.cast(in.getValue(this)), true);
            } catch (IllegalArgumentException ie) {
                log(ie.getMessage(), MSG.WARNING);
            }
        } if (startTrigger!=null && startTrigger.matches(in, channel, this)) {
            if (frameTimer!=null)
                log("Received a start stream trigger while streaming is already active.", MSG.WARNING);
            else startStream();
        } if (stopTrigger!=null && stopTrigger.matches(in, channel, this)) {
            if (frameTimer==null)
                log("Received a stop stream trigger while not streaming", MSG.WARNING);
            else stopStream();
        }
    }
    
    @Override
    public void start() {
        super.start();
        if (startTrigger==null) startStream();
    }

    @Override
    public void stop() {
        stopStream();
        super.stop();
    }

    private void stopStream() {
        if (frameTimer==null) return;

        frameTimer.cancel();
        frameTimer = null;
    }

    private void startStream() {
        if (frameTimer!=null) return;
        frameTimer = new Timer();
        frameTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                sendToOutputs(streamVar, -1);
            }
        }, 0, sampleRate);
    }
}
